﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace ChatApplication_Wpf
{
    /// <summary>
    /// Interaction logic for HeaderPage.xaml
    /// </summary>
    public partial class HeaderPage : UserControl
    {
        App app = Application.Current as App;
        public HeaderPage()
        {
            InitializeComponent();
            LoadHeader();
        }

        private void LoadHeader()
        {
            ChatServerEntities1 entities = new ChatServerEntities1();
            var result = (from d in entities.User_Details
                          where d.User_Id == app.Username
                          select d).SingleOrDefault();

            #region "insert image"
            //FileStream stream = new FileStream("C:\\Temp\\1.png", FileMode.Open);
            //byte[] image = new byte[stream.Length];
            //stream.Read(image, 0, Convert.ToInt32(stream.Length));
            //stream.Close();

            //result.User_Picture = image;
            //entities.SaveChanges();

            #endregion

            if (result != null)
            {
                byte[] image = (byte[])result.User_Picture;
                MemoryStream fs = new MemoryStream(image);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = fs;
                bi.EndInit();
                image1.Source = bi;
                if (result != null)
                {
                    SolidColorBrush colour = null;
                    switch (result.Login_Status)
                    {
                        case "Online":
                            colour = Brushes.Green;
                            break;
                        case "Offline":
                            colour = Brushes.Red;
                            break;
                        default:
                            colour = Brushes.Orange;
                            break;
                    }
                    this.DataContext = new ChatProperties() { Name=result.User_Id, UserStatus = colour, Status = result.Login_Status };
                    
                }
            }
        }
    }
}
